package Javaa.Base.Thread;
//create and example of Callable usage in Java class functionality
// demonstrate returning values from threads
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; 
public class CallableExample {
    public static void main(String[] args) {
        // Create a thread pool with 2 threads
        ExecutorService executor = Executors.newFixedThreadPool(2);

        // Create Callable tasks
        Callable<Integer> task1 = () -> {
            System.out.println("Executing Task 1");
            Thread.sleep(1000); // Simulate work
            return 10;
        };

        Callable<Integer> task2 = () -> {
            System.out.println("Executing Task 2");
            Thread.sleep(2000); // Simulate work
            return 20;
        };

        try {
            // Submit tasks and get Future objects
            Future<Integer> future1 = executor.submit(task1);
            Future<Integer> future2 = executor.submit(task2);

            // Retrieve results from Future objects
            Integer result1 = future1.get(); // This will block until the result is available
            Integer result2 = future2.get(); // This will block until the result is available

            System.out.println("Result of Task 1: " + result1);
            System.out.println("Result of Task 2: " + result2);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            // Shutdown the executor
            executor.shutdown();
        }
    }
}